<template>
    <div>
        <a-form layout="inline"
                :model="searchData"
                style="margin: 10px 0">
            <a-form-item>
                <a-input v-model:value="searchData.username" placeholder="用户名称"></a-input>
            </a-form-item>
            <a-form-item>
                <a-select
                        ref="select"
                        v-model:value="searchData.deptId"
                        placeholder="所属部门"
                        style="width: 120px"
                        :options="deptList"
                        :field-names="{label:'name',value:'id'}"
                ></a-select>
            </a-form-item>
            <a-form-item>
                <a-select
                        ref="select"
                        v-model:value="searchData.roleId"
                        placeholder="所属角色"
                        style="width: 120px"
                        :options="roleList"
                        :field-names="{label:'name',value:'id'}"
                ></a-select>
            </a-form-item>
            <a-form-item>
                <a-button type="primary" @click="load">搜索</a-button>
            </a-form-item>
            <a-form-item>
                <a-button type="default" @click="rest">重置</a-button>
            </a-form-item>
        </a-form>
        <a-button type="primary" style="margin-bottom: 10px"
                  @click="openModal({password:'123'})">
            新增
        </a-button>
        <a-table :dataSource="userList"
                 :columns="columns"
                 :pagination="pagination"
                 @change="handlerChange"
                 bordered>
            <template #avatar="{text,record}">
                <img :src="text" v-if="text" style="width: 50px;height: 50px;border-radius: 50%"/>
                <img src="https://lyj-mall-bucket.oss-cn-guangzhou.aliyuncs.com/20230129/default.png" v-else style="width: 50px;height: 50px;border-radius: 50%"/>
            </template>
            <template #deptId="{text,record}">
                {{beLongDept(record.deptId)}}
            </template>
            <template #roleId="{text,record}">
                {{beLongRole(text)}}
            </template>
            <template v-slot:action="{text,record}">
                <a-button type="primary" @click="openModal(record)">编辑</a-button>
                <a-popconfirm
                        title="删除数据无法恢复，请确认是否删除？"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="okDelete(record.id)"
                >
                    <a-button type="danger">删除</a-button>
                </a-popconfirm>
            </template>
        </a-table>
        <a-modal v-model:visible="visible" title="新增或编辑" @ok="handleOk">
            <a-form v-model="user"
                    :label-col="{ span: 8 }">
                <a-form-item label="账号">
                    <a-input v-model:value="user.usercode" :disabled="user.id"></a-input>
                </a-form-item>
                <a-form-item label="用户名称">
                    <a-input v-model:value="user.username"></a-input>
                </a-form-item>
                <a-form-item label="所属部门">
                    <a-select
                            ref="select"
                            v-model:value="user.deptId"
                            placeholder="请选择"
                            style="width: 120px"
                            :options="deptList"
                            @change="handlerSelected"
                            :field-names="{label:'name',value:'id'}"
                    ></a-select>
                </a-form-item>
                <a-form-item label="所属角色">
                    <a-select
                            ref="select"
                            v-model:value="user.roleId"
                            placeholder="请选择"
                            style="width: 120px"
                            :disabled="roleData.length == 0"
                            :options="roleData"
                            :field-names="{label:'name',value:'id'}"
                    ></a-select>
                </a-form-item>
            </a-form>
        </a-modal>
    </div>
</template>

<script>
    import {ref, onMounted,reactive} from 'vue'
    import axios from 'axios'
    import {message} from 'ant-design-vue';

    export default {
        name: "personnel-user",
        setup() {
            const columns = [
                {
                    title: 'ID',
                    dataIndex: 'id',
                    key: 'id',
                    align: 'center'
                },
                {
                    title: '头像',
                    dataIndex: 'avatar',
                    key: 'avatar',
                    slots: {customRender: 'avatar'},
                    align: 'center'
                },
                {
                    title: '用户账号',
                    dataIndex: 'usercode',
                    key: 'usercode',
                    align: 'center'
                },
                {
                    title: '用户名',
                    dataIndex: 'username',
                    key: 'username',
                    align: 'center'
                },
                {
                    title: '所属部门',
                    dataIndex: 'deptId',
                    key: 'deptId',
                    slots: {customRender: 'deptId'},
                    align: 'center'
                },
                {
                    title: '担任角色',
                    dataIndex: 'roleId',
                    key: 'roleId',
                    slots: {customRender: 'roleId'},
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    slots: {customRender: 'action'},
                    align: 'center'
                }
            ]

            const user = ref({})
            const userList = ref()
            const deptList = ref()
            const roleList = ref()
            const roleData = ref([])
            const searchData = ref({})
            const pagination = ref({
                current: 1,
                pageSize: 5,
            })

            const visible = ref(false);

            const load = () => {
                axios.get('/user/list', {
                    params: {
                        pageNum: pagination.value.current,
                        pageSize: pagination.value.pageSize,
                        username: searchData.value.username,
                        deptId: searchData.value.deptId,
                        roleId: searchData.value.roleId
                    }
                }).then(rs => {
                    console.log("查找结果：", rs)
                    userList.value = rs.data.data.list
                    pagination.value.total = rs.data.data.total
                })
            }

            const rest = () => {
                searchData.value = {}
                load()
            }

            const handlerChange = (pageInfo) => {
                pagination.value = pageInfo
                load()
            }

            const openModal = (obj) => {
                console.log("模态框参数：", obj)
                visible.value = true;
                user.value = JSON.parse(JSON.stringify(obj))
                if (typeof(user.value.id) == 'undefined'){
                    roleData.value = []
                }else {
                    handlerSelected(user.value.deptId)
                }
            }

            const handleOk = () => {
                axios.post('/user/saveOrUpdate', user.value).then(rs => {
                    console.log("新增或修改结果：", rs)
                    if (rs.data.code == 200) {
                        visible.value = false
                        message.success("操作成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const okDelete = (id) => {
                console.log("要删除的ID：" + id)
                axios.delete('/user/delete/' + id).then(rs => {
                    if (rs.data.code == 200) {
                        message.success("删除成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const beLongDept = (id) => {
                if (typeof (id) == "undefined") {
                    return "未分配部门"
                }
                let rs = '未知角色'
                deptList.value.forEach((val, index) => {
                    if (val.id == id) {
                        rs = val.name
                    }
                })
                return rs
            }
            const beLongRole = (id) => {
                if (typeof (id) == "undefined") {
                    return "未分配角色"
                }
                let rs = '未知角色'
                roleList.value.forEach(val => {
                    if (val.id == id) {
                        rs = val.name
                    }
                })
                return rs
            }

            const handlerSelected = value =>{
                roleData.value = []
                roleList.value.forEach((val,index) =>{
                    if (val.deptId == value){
                        roleData.value.push(val)
                    }
                })
            }

            onMounted(() => {
                axios.get('/dept/queryAll').then(rs => {
                    deptList.value = rs.data.data
                })
                axios.get('/role/queryAll').then(rs => {
                    roleList.value = rs.data.data
                })
                load()
            })
            return {
                columns,
                user,
                userList,
                pagination,
                handlerChange,
                searchData,
                load,
                rest,
                openModal,
                visible,
                handleOk,
                okDelete,
                beLongDept,
                beLongRole,
                deptList,
                roleList,
                roleData,
                handlerSelected
            }
        }
    }
</script>

<style scoped>

</style>